package qsoft.omanga.base.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import qsoft.omanga.common.utils.DataConfig;

/**
 * 
 * @author sontt
 * 
 */
public class DBHelper {

    /**
     * 
     */
    private MangaDBHelper mangaDBHelper;
    /**
     * 
     */
    private SQLiteDatabase db;

    /**
     * @param context
     *            Context
     */
    public DBHelper(Context context) {
        mangaDBHelper = new MangaDBHelper(context);
    }

    /**
     * @return DBHelper
     * @throws SQLException
     *             if the database cannot be opened for writing
     */
    public DBHelper open() throws SQLException {
        db = mangaDBHelper.getWritableDatabase();
        return this;
    }

    /**
     * @category close DBHelper
     */
    public void close() {
        mangaDBHelper.close();
    }

    /**
     * @param createtable
     *            String
     */
    public void create(String createtable) {
        db.execSQL(createtable);
    }

    /**
     * @author sontt
     * @param tableName
     *            String
     * @param contentValues
     *            ContentValues
     * @return long
     */
    public long insert(String tableName, ContentValues contentValues) {
        long result = -1;
        try {
            result = db.insertOrThrow(tableName, null, contentValues);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }

    /**
     * @param tableName
     *            String
     * @param values
     *            ContentValues
     * @param whereClause
     *            String
     * @param whereArgs
     *            String[]
     * @return long
     */
    public long update(String tableName, ContentValues values,
            String whereClause, String[] whereArgs) {
        return db.update(tableName, values, whereClause, whereArgs);
    }

    /**
     * @param tableName
     *            String
     * @param whereClause
     *            String
     * @param whereArgs
     *            String[]
     * @return long
     */
    public long delete(String tableName, String whereClause, String[] whereArgs) {
        return db.delete(tableName, whereClause, whereArgs);
    }

    /**
     * @param tableName
     *            String
     * @param columns
     *            String[]
     * @param order
     *            String
     * @return Cursor
     */
    public Cursor getAll(String tableName, String[] columns, String order) {
        Cursor cur = db
                .query(tableName, columns, null, null, null, null, order);
        return cur;
    }

    /**
     * @param tableName
     *            String
     * @param columns
     *            String[]
     * @param columnID
     *            String
     * @param rowId
     *            long
     * @return Cursor
     * @throws SQLException
     *             SQLException
     */
    public Cursor getRowID(String tableName, String[] columns, String columnID,
            long rowId) throws SQLException {
        Cursor cursor = db.query(true, tableName, columns, columnID + "="
                + rowId, null, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

    /**
     * @param tableName
     *            String
     * @param columns
     *            String[]
     * @param where
     *            String
     * @param order
     *            String
     * @return cursor
     * @throws SQLException
     *             SQLException
     */
    public Cursor getCondition(String tableName, String[] columns,
            String where, String order) throws SQLException {
        Cursor cursor = db.query(true, tableName, columns, where, null, null,
                null, order, null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

    /**
     * 
     */
    private class MangaDBHelper extends SQLiteOpenHelper {

        /**
         * @param context
         *            Context
         */
        public MangaDBHelper(Context context) {
            super(context, DataConfig.DATABASE_NAME, null,
                    DataConfig.DATABASE_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase database) {
            database.execSQL(DataConfig.CREATE_TABLE_OMANGA);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DataConfig.TABLE_MANGA);
            onCreate(db);
        }
    }
}
